<script>
    // angularjs模块定义
    //, 'JxApp.mulsel', 'JxApp.filter'
    var goodsApp = angular.module('JxApp', ["ui.bootstrap.datepicker", "ui.bootstrap.tpls", "JxApp.mulsel"]);

    goodsApp.filter('attrDisabledFilter', function ($filter) {
	    return function (attrs) {
		    var result = [];
		    for (var i = 0; i < attrs.length; i++) {
			    if (attrs[i].status == 1) {
				    result.push(attrs[i]);
			    }
		    }
		    return result;
	    };
    });

    goodsApp.controller("ColorsFormModule", function ($scope, $http, $filter) {
    	//获取全部尺码、颜色数据
	    $scope.allSizes = allSizes;
	    $scope.allColors = allColors;

		$scope.recordTypes = recordTypeListJson;
	    //初始化
	    $scope.searchForm = {};
	    var vToday = new Date();
	    $scope.searchForm.createTimeEnd = vToday;
	    // 算上今天，倒推29天
	    $scope.searchForm.createTimeBegin = addDate(vToday, -29);
	    $scope.businessItems = GL_CONS.BANLANCE_BUSINESS_ITEM;//所有业务类型常量数组

	    $scope.businessSnRef = GL_CONS.BANLANCE_BUSINESS_SN_REF;

	    $scope.regUpperAlphabet = /^[A-Z]*/;

	    /**
	     * 如果为库存成本算法非全部加权平均需要显示库存成本流水
	     */
	    $scope.showGoodsCostRecord = function () {
		    if ($scope.resources.indexOf('goodsStoredPurchasePrice') == -1) {
			    return false;
		    }
		    if ($scope.goodsCostAlgorithm != GL_CONS.GOOD_COST_ALGORITHM_VAL.AP) {
			    return true;
		    }
		    return false;
	    };

	    /**
	     * 点击查询
	     */
	    $scope.search = function () {
		    var postData = {
			    goodsSn: $scope.searchForm.goodsSn,
			    colorId: $scope.searchForm.colorId,
			    sizeId: $scope.searchForm.sizeId,
				recordType:$scope.searchForm.recordType,
			    updateTimeBegin: $scope.searchForm.createTimeBegin,
			    updateTimeEnd: $scope.searchForm.createTimeEnd
		    };
		    $scope.queryingData = true;
		    $http.post('{{ route('admin.shop.record.goodsStoredRecordList') }}', postData).success(function (result) {
			    $scope.queryingData = false;
			    $scope.dataList = result.data;
		    }).error(function (data, status) {
			    $scope.queryingData = false;
		    });
	    };

	    $scope.$watch('searchForm.good', function () {
		    if ($scope.searchForm.good != undefined && $scope.searchForm.good != '') {
			    var currentColorIds = $scope.searchForm.good.colorIds ? $scope.searchForm.good.colorIds.split(',') : [];
			    var currentSizeIds = $scope.searchForm.good.sizeIds ? $scope.searchForm.good.sizeIds.split(',') : [];
			    $scope.currentColors = [];
			    $scope.currentSizes = [];

			    for (var i = 0; i < currentColorIds.length; i++) {
				    var color = getObject($scope.allColors, 'id', parseInt(currentColorIds[i]));
				    if (color) {
					    color.val = color.id;
					    color.text = color.name;
					    $scope.currentColors.push(color);
				    }
			    }

			    for (var i = 0; i < currentSizeIds.length; i++) {
				    var size = getObject($scope.allSizes, 'id', parseInt(currentSizeIds[i]));
				    if (size) {
					    size.val = size.id;
					    size.text = size.name;
					    $scope.currentSizes.push(size);
				    }
			    }

			    $scope.currentColors = $filter("orderBy")($filter("attrDisabledFilter")($scope.currentColors), ['showOrder', 'id']);
			    $scope.currentSizes = $filter("orderBy")($filter("attrDisabledFilter")($scope.currentSizes), ['showOrder', 'id']);

			    $scope.searchForm.colorId = '';
			    $scope.searchForm.sizeId = '';
		    }
	    });
    });

    /*回车事件*/
    $('#searchForm').keypress(function (e) {
	    if (e.keyCode == '13') {
		    stopEvent(e);
		    if (getScope("recordCtrl").searchForm.depotId && getScope("recordCtrl").searchForm.goodsSn) {
			    $('#searchBtn').click();
		    }
	    }
    });
</script>